package com.hj.ct.fragment;

import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;

import android.content.Intent;
import android.graphics.Color;
import android.graphics.Typeface;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemSelectedListener;
import android.widget.Spinner;

import com.github.mikephil.charting.charts.PieChart;
import com.github.mikephil.charting.data.Entry;
import com.github.mikephil.charting.data.PieData;
import com.github.mikephil.charting.data.PieDataSet;
import com.github.mikephil.charting.utils.ColorTemplate;
import com.github.mikephil.charting.utils.PercentFormatter;
import com.hj.ct.App;
import com.hj.ct.R;
import com.hj.ct.activity.LoginActivity;
import com.hj.ct.adapter.SpinnerAdapter;
import com.hj.ct.logic.GatherDataLogic;
import com.hj.ct.logic.LogicActions;
import com.hj.ct.parse.Report3Data;

public class BarChart3Fragment extends BaseFragment implements OnItemSelectedListener {

	protected PieChart mChart1;
	private Typeface tf;
	private View loadingLayout;
	private Spinner time;
	private Spinner device;
	private SpinnerAdapter deviceAdapter;
	private long currentTime = System.currentTimeMillis();
	@Override
	public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
		tf = Typeface.createFromAsset(getActivity().getAssets(), "OpenSans-Regular.ttf");
		View view = inflater.inflate(R.layout.fragment_chart3, null);
		mChart1 = (PieChart) view.findViewById(R.id.chart3);
		loadingLayout = view.findViewById(R.id.loading);
		time = (Spinner) view.findViewById(R.id.time_long);
		SpinnerAdapter adapter = new SpinnerAdapter(getActivity());
		adapter.setList(Arrays.asList("实时值", "小时均值", "日均值"));
		time.setAdapter(adapter);
		time.setOnItemSelectedListener(this);

		device = (Spinner) view.findViewById(R.id.device);
		deviceAdapter = new SpinnerAdapter(getActivity());
		device.setAdapter(deviceAdapter);
		device.setOnItemSelectedListener(this);
		device.setVisibility(View.GONE);
		initBarChart(mChart1);
		loading();
		GatherDataLogic.getInstance().getChart3(this, currentTime, 1);
		return view;
	}

	public void setCurrentTime(long time){
		this.currentTime = time;
		this.time.setSelection(0);
		loading();
		GatherDataLogic.getInstance().getChart3(this, currentTime, 1);
	}
	
	private void loading() {
		loadingLayout.setVisibility(View.VISIBLE);
	}

	private void closeLoading() {
		loadingLayout.setVisibility(View.GONE);
	}

	private void initBarChart(PieChart barChart) {
		mChart1.setNoDataTextDescription("暂无数据");
		mChart1.setCenterTextTypeface(tf);
//		mChart1.setUsePercentValues(true);
//		mChart1.setDrawHoleEnabled(true);
		mChart1.setRotationAngle(0);
		mChart1.setRotationEnabled(true);
	}

	public void onItemSelected(AdapterView<?> parent, View view, int position, long id) {
		if (parent == time) {
			loading();
			GatherDataLogic.getInstance().getChart2(this, currentTime, position + 1);
		} else if (parent == device) {
			setData(report3Data, position);
		}

	}

	public void onNothingSelected(AdapterView<?> parent) {

	}

	private void setData(List<Report3Data> datas, int position) {
		if (datas == null) {
			return;
		}
		ArrayList<Entry> yVals1 = new ArrayList<Entry>();
		ArrayList<String> xVals = new ArrayList<String>();

		Report3Data data3 = datas.get(position);
		xVals.add("<0.3");
		xVals.add("<0.5");
		xVals.add("<1.0");
		xVals.add(">1.0");
		yVals1.add(new Entry(data3.getDust0(), 0));
		yVals1.add(new Entry(data3.getDust1(), 1));
		yVals1.add(new Entry(data3.getDust2(), 2));
		yVals1.add(new Entry(data3.getDust3(), 3));

		PieDataSet dataSet = new PieDataSet(yVals1, "粉尘浓度分布(mg/m3)");
		dataSet.setSliceSpace(3f);
		dataSet.setSelectionShift(5f);

		// add a lot of colors
		ArrayList<Integer> colors = new ArrayList<Integer>();
		for (int c : ColorTemplate.PASTEL_COLORS)
			colors.add(c);
		for (int c : ColorTemplate.JOYFUL_COLORS)
			colors.add(c);
		for (int c : ColorTemplate.COLORFUL_COLORS)
			colors.add(c);
		
		for (int c : ColorTemplate.VORDIPLOM_COLORS)
			colors.add(c);
		for (int c : ColorTemplate.LIBERTY_COLORS)
			colors.add(c);
		
		colors.add(ColorTemplate.getHoloBlue());
		dataSet.setColors(colors);

		PieData data = new PieData(xVals, dataSet);
		data.setValueFormatter(new PercentFormatter());
		data.setValueTextSize(11f);
		data.setValueTextColor(Color.BLACK);
		data.setValueTypeface(tf);
//		data.setDrawValues(false);
		mChart1.setData(data);
//		mChart1.highlightValues(null);
		mChart1.notifyDataSetChanged();
		mChart1.invalidate();
	}

	private List<Report3Data> report3Data;

	public void handlerUI(int action, int code, Object... obj) {
		closeLoading();
		if (action == LogicActions.REPORT3_DATA_SUCCESS) {
			report3Data = (List<Report3Data>) obj[0];
			if (report3Data == null)
				return;
			setData(report3Data, 0);
			initDevice();
		} else if (action == LogicActions.REPORT3_DATA_ERROR) {
			toast(obj[0] + "");
		} else if (action == LogicActions.NEED_TO_LOGIN) {
			showReLoginDialog();
		}
	}

	public void initDevice() {
		if (report3Data == null)
			return;
		ArrayList<String> devices = new ArrayList<String>();
		for (Report3Data data : report3Data) {
			devices.add("设备" + data.getDeviceID());
		}
		if (devices.size() > 0) {
			device.setVisibility(View.VISIBLE);
		}else{
			device.setVisibility(View.GONE);
		}
		device.setSelection(0);
		deviceAdapter.setList(devices);
		deviceAdapter.notifyDataSetChanged();
	}

}
